/*
ping工具包
*/
package nettool

import (
	"log"
	"net"
	"net/http"
	"strings"
	"time"
)

// 测试检测是否存活
func SurviveAlive(client *http.Client, url string) bool {
	resp, err := client.Get(url)
	if err != nil {
		log.Println("[LOG] PollingSurvive failed")
		return false
	}
	return resp.StatusCode == 200
}

// 测试服务是否可用 IP:PORT
func PingIPPort(host string) bool {
	var (
		ip  string
		err error
	)
	//获取IP PORT
	if strings.Contains(host, "http://") {
		ip = host[len("http://"):]
	} else if strings.Contains(host, "https://") {
		ip = host[len("https://"):]
	} else {
		ip = host
	}
	var conn net.Conn
	if conn, err = net.DialTimeout("tcp", ip, 3*time.Second); err != nil {
		log.Println("[LOG] error:", err)
		return false
	}
	conn.Close()

	return true
}
